package com.rc.saas.tenant.controller.elec;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.TenantBaseController;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.utils.QrCodeUtils;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.model.area.PlatformProvince;
import com.rc.saas.tenant.model.elec.ElecGun;
import com.rc.saas.tenant.model.elec.ElecPile;
import com.rc.saas.tenant.model.elec.ElecStation;
import com.rc.saas.tenant.model.elec.RcEvaluate;
import com.rc.saas.tenant.model.hlht.HlhtTenantOpenfor;
import com.rc.saas.tenant.model.operator.RcOperator;
import com.rc.saas.tenant.model.order.OrderInfo;
import com.rc.saas.tenant.model.order.OrderInfoExample;
import com.rc.saas.tenant.model.tenant.TenantCompanyInfo;
import com.rc.saas.tenant.model.tenant.TenantRole;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.search.elec.ElecGunSearch;
import com.rc.saas.tenant.search.elec.RcEvaluateSearch;
import com.rc.saas.tenant.search.order.OrderSearch;
import com.rc.saas.tenant.search.tenant.TenantRoleSearch;
import com.rc.saas.tenant.service.area.PlatformAreaService;
import com.rc.saas.tenant.service.elec.ElecGunService;
import com.rc.saas.tenant.service.elec.ElecPileService;
import com.rc.saas.tenant.service.elec.ElecStationService;
import com.rc.saas.tenant.service.elec.RcEvaluateService;
import com.rc.saas.tenant.service.hlht.HlhtTenantOpenforService;
import com.rc.saas.tenant.service.operator.RcOperatorService;
import com.rc.saas.tenant.service.order.OrderInfoService;
import com.rc.saas.tenant.service.tenant.TenantCompanyInfoService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import com.rc.saas.tenant.util.ExportUtils;
import com.rc.saas.tenant.vo.elec.ElecStationAndCityName;
import com.rc.saas.tenant.vo.elec.RcEvaluateVo;
import com.rc.saas.tenant.vo.order.OrderResult;
import groovy.lang.Lazy;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * zhoushiyu
 * 站标签
 */
@Controller
@RequestMapping("/evaluate")
public class RcEvaluateController extends TenantBaseController {
    private static Logger logger = LogManager.getLogger(RcEvaluateController.class);


    @Autowired
    private TenantCompanyInfoService tenantCompanyInfoService;

    @Autowired
    private RcEvaluateService rcEvaluateService;

    @Autowired
    private ElecStationService elecStationService;

    @Autowired
    private OrderInfoService orderInfoService;


    /**
     * 跳转站标签页面
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String elecStationPage(Model model) {
        return "/templates/elec/rcEvaluate";
    }

    /**
     * 返回充电评价列表
     *
     * @return
     */
    @RequestMapping(value = "/listRcEvaluate", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listRcEvaluate(RcEvaluateSearch rcEvaluateSearch) {
        PageInfo<RcEvaluateVo> pageInfo = rcEvaluateService.listByCondition(rcEvaluateSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }


    /**
     * 导出充电评价列表
     */
    @ResponseBody
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public TResponse export(RcEvaluateSearch rcEvaluateSearch, HttpServletResponse response) {
        try {
            response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            ExportUtils.setDefaultPaging(rcEvaluateSearch);
            PageInfo<RcEvaluateVo> pageInfo = rcEvaluateService.listByCondition(rcEvaluateSearch);
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("充电评价列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), RcEvaluateVo.class).sheet("充电评价列表").doWrite(pageInfo.getList());
            return null;
        } catch (Exception e) {
            logger.error("导出充电评价列表失败", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }
    }
/*

    */
/**
     * 跳转编辑桩群
     *
     * @return
     *//*

    @RequestMapping(value = "/updateStatus/{elecGunId}", method = RequestMethod.GET)
    public String updateStatus(@PathVariable("elecGunId") Long elecGunId, Model model) {
        TenantUser user = ShiroUtils.getSessionUser();
        ElecGun info = elecGunService.selectByPrimaryKey(elecGunId);
        model.addAttribute("elecGun", info);
        List<ElecPile> piles = elecPileService.selectListPiles(user.getTenantCode());
        model.addAttribute("piles", piles);
        return "/templates/elec/editElecGun";
    }

    */

    /**
     * 保存审核
     * @param evaluateId
     * @return
     */

    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @ResponseBody
    public TResponse updateStatus(Long evaluateId,String status,String remark) {
        RcEvaluate info = rcEvaluateService.selectByPrimaryKey(evaluateId);
        if (info==null){
            return TResponse.FAIL("请求错误");
        }
        info.setStatus(status);
        info.setRemark(remark);
        rcEvaluateService.updateByPrimaryKey(info);
        return TResponse.SUCCESS();
    }

    /**
     * 删除
     * @param evaluateId
     * @return
     */

    @RequestMapping(value = "/deleteRcEvaluate", method = RequestMethod.POST)
    @ResponseBody
    public TResponse deleteRcEvaluate(Long evaluateId) {
        RcEvaluate info = rcEvaluateService.selectByPrimaryKey(evaluateId);
        if (info==null){
            return TResponse.FAIL("请求错误");
        }
        rcEvaluateService.deleteByPrimaryKey(evaluateId);
        return TResponse.SUCCESS();
    }

    /**
     * 跳转评价详情
     *
     * @return
     */

    @RequestMapping(value = "/detailRcEvaluate/{evaluateId}", method = RequestMethod.GET)
    public String detailRcEvaluate(@PathVariable("evaluateId") Long evaluateId, Model model) {
        RcEvaluate info = rcEvaluateService.selectByPrimaryKey(evaluateId);
        model.addAttribute("info", info);
        if (info.getElecStationId()!=null){
            ElecStation station = elecStationService.selectByPrimaryKey(info.getElecStationId());
            model.addAttribute("stationInfo", station);
        }
        /*if (info.getOrderId()!=null){
          *//*  OrderSearch orderSearch = new OrderSearch();
            orderSearch.setOrderId(info.getOrderId());
            List<OrderResult> order =orderInfoService.listByCondition(orderSearch);
            if (order.size()>0){
                model.addAttribute("orderInfo", order.get(0));
            }
*//*
        }*/
        return "/templates/elec/detailRcEvaluate";
    }

    /**
     * 返回充电详情table
     *
     * @return
     */
    @RequestMapping(value = "/ByIdForOrder", method = RequestMethod.GET)
    @ResponseBody
    public List<OrderResult> ByIdForOrder(OrderSearch orderSearch) {
        /*PageInfo<RcEvaluateVo> pageInfo = rcEvaluateService.listByCondition(rcEvaluateSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());*/
        return orderInfoService.listByCondition(orderSearch);
    }

/**
     *
     * 小程序二维码
     * @param gunNo
     * @return
     *//*

    @RequestMapping(value = "/genElecGunQrCode/{gunNo}", method = RequestMethod.GET)
    public String genElecGunQrCode(@PathVariable("gunNo") String gunNo, Model model) {
        TenantUser user = ShiroUtils.getSessionUser();
        if (user == null) {
            return null;
        }
        String url = QrCodeUtils.createCecQRCode(user.getTenantCode(), gunNo);
        model.addAttribute("qrcode", url);

        //小程序二维码生成
        String miniQrcode = QrCodeUtils.createMiniQRCode(user.getTenantCode(), gunNo);
        model.addAttribute("miniQrcode", miniQrcode);

        //新增第三方二维码
        HlhtTenantOpenfor hlhtTenantOpenfor = hlhtTenantOpenforService.selectTenantOpenforByGunNo(user.getTenantCode(), gunNo);
        if (hlhtTenantOpenfor != null) {
            String hlhtQrcode = QrCodeUtils.createCecQRCode(hlhtTenantOpenfor.getHlhtTenantCode(), gunNo);
            model.addAttribute("hlhtQrcode", hlhtQrcode);
        }


        //微信公众号网页授权
        //ElecGun elecGun = elecGunService.selectGunByGunNo(gunNo);
        //String wechatQrcode = QrCodeUtils.createWechatQRCode(user.getTenantCode(), gunNo, elecGun.getElecStationId().toString());
        //model.addAttribute("qrcodeWechat", wechatQrcode);

        return "/templates/elec/elecGunQrcode";
    }
*/

}
